<?php
/**
 * Created by PhpStorm.
 * User: haobin
 * Date: 2016/10/24
 * Time: 15:52
 * Description： 扣量页面
 */
header("Access-Control-Allow-Origin: *");//允许跨域请求
require('../conn.php');
require('../functions.php');
$uid = $_GET['uid'];
$aid = $_GET['aid'];
$device = $_GET['device'];
$domain = $_GET['domain'];
$platform = $_GET['platform'];
$ip = GetIP();
$day = date("Y-m-d", time());
//权限判断
$user = $mysql->query("select * from `user` WHERE `id`={$uid}");
$userinfo = $user[0];
if ($userinfo['ban'] != 0) {
    exit;
}
//是否已存在该ip,如果有就跳过
$row_ip = $mysql->query("select `id` from `sharedata` where `day`='{$day}' AND `ip`='{$ip}'");
if ($row_ip) {
    exit;
}
//积分数组20
$arr_refer = [
    'uid' => $uid,
    'aid' => $aid,
    'money' => 20,
    'ip' => $ip,
    'day' => $day,
    'time' => date("H:i:s", time()),
    'device' => $device,
    'platform' => $platform,
    'domain' => $domain,
    'type' => 0,
];
//扣量数组,type=1为扣量
$arr_deduct = [
    'uid' => $uid,
    'aid' => $aid,
    'money' => 20,
    'ip' => $ip,
    'day' => $day,
    'time' => date("H:i:s", time()),
    'device' => $device,
    'platform' => $platform,
    'domain' => $domain,
    'type' => 1,
];
//扣量百分比
$deduction = 30;
if (rand(0, 100) < $deduction) {
    //扣量
    $res = $mysql->execute("insert into `sharedata`" . arr2s($arr_deduct));
} else {
    //正常增加收入(user的total、score，jhi_user_authority的share_friend)
    $res = $mysql->execute("insert into `sharedata`" . arr2s($arr_refer));
    $res = $mysql->execute("update `user` set `total`=`total`+20,`score`=`score`+20 WHERE `id`={$uid}");
    $res = $mysql->execute("update `jhi_user_authority` set `share_friend`=`share_friend`+20 WHERE `user_id`={$uid}");
    //增加师傅收入
    if ($userinfo['pid']) {
        $pInfo = $mysql->query("select * from `user` WHERE `id`={$userinfo['pid']}");
        $pRefer = [
            'uid' => $userinfo['pid'],
            'aid' => $aid,
            'money' => 2,
            'ip' => $ip,
            'day' => $day,
            'time' => date("H:i:s", time()),
            'device' => $device,
            'platform' => $platform,
            'domain' => $domain,
            'type' => 2,
        ];
        $res = $mysql->execute("insert into `sharedata`" . arr2s($pRefer));
        $res = $mysql->execute("update `user` set `total`=`total`+2,`score`=`score`+2 WHERE `id`={$userinfo['pid']}");
        $res = $mysql->execute("update `jhi_user_authority` set `share_friend`=`share_friend`+2 WHERE `user_id`={$userinfo['pid']}");
    }
    echo $res;
}


